package edu.scau.hibernate.text;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import edu.scau.hibernate.model.User;

public class HibernateTest {

	public static void main(String[] args) {
		Configuration cfg=new Configuration().configure();   //读取hibernate.cfg.xml文件
		@SuppressWarnings("deprecation")
		SessionFactory factory=cfg.buildSessionFactory();   //建立SessionFactory
		Session session=null;   //取得session
		try{
			session=factory.openSession();   
			session.beginTransaction();    //开启事物
			User user=new User();
			user.setUserName("yyl");
			user.setPassword("321");
			session.save(user);   //保存User对象
			session.getTransaction().commit();
		}catch(Exception e){
			e.printStackTrace();
			session.getTransaction().rollback();
		}finally{
			if(session!=null){
				if(session.isOpen()){
					session.close();
				}
			}
		}

	}

}
